package aaBaolilo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.CmdX;

public class OcrX {

	public static void main(String[] args) {
 //  "C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" e:\a.jpg e:\a_ok.jpg -l chi_sim
		String baseName="a";
		String dir="c:\\bb";
		 File dir2 = new File(dir); 
		    File[] files = dir2.listFiles(); 
		 for (File file : files) {
			
		   
			ImageDemo demo = new ImageDemo();	
			String bname=filex.getFileName_noExtName(file.getAbsolutePath());
			String exname=filex.getExtName(file.getAbsolutePath());
			String bin_jpg=dir+"\\"+bname+"_bin."+exname;
			try {
				demo.binaryImage(file.getAbsolutePath(),bin_jpg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(getCaptch(bin_jpg));
			
//			String cmd="\"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe\" e:\\"+baseName+".jpg e:\\"+baseName+"_ok.jpg -psm 8";
//			String ret=CmdX.exec(cmd);
//			System.out.println(ret);
//			 baseName=nextChar(baseName);
		}


	}
	public static String getCaptch(String jpg) {
		String baseName=filex.getFileName_noExtName(jpg);
		File f=new File(jpg);
		String parentPath=f.getParentFile().getAbsolutePath();
		String newFile=parentPath+"\\"+baseName;
		String newfile2=newFile+".txt";
		
		
String exepath=pathx.classPathParent()+"\\Tesseract\\tesseract.exe";
		String cmd="\""+exepath+"\"  \""+jpg+"\" \""+newFile+"\"  -psm 7";
		System.out.println(cmd);
	List<String> li=new ArrayList();
	li.add(exepath);li.add(jpg);li.add(newFile);li.add("-psm");li.add("7");
		
		String ret=CmdX.exec_EX(cmd,li);
		String t=filex.read_NSF(newfile2).trim();
		return t;
		
	}
	

	public static String nextChar(String baseName) {
		byte[] ca=baseName.getBytes();
		int now=ca[0];
		now++;
		char c=(char) now;
		
		return String.valueOf(c);
	}

}
